package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrdersettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrdersettingDao ordersettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;


    @Override
    public Result add(Map<String, String> map)throws Exception {
        
        //判断指定日期是否可以预约
        String date = map.get("orderDate");
        Date orderDate=DateUtils.parseString2Date(date);

        //根据日期查询OrderSetting对象，如果orderSetting对象不为空说明可以预约，否则不可以预约
        OrderSetting orderSetting=ordersettingDao.findOrderSettingByDate(orderDate);

        if(orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //判断当前日期预约人数是否已满
        if(orderSetting.getReservations()>=orderSetting.getNumber()){
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //根据手机号判断当前用户是不是会员
        Member member= memberDao.findMemberByTelephone(map.get("telephone"));

        if(member==null){   //该用户还是不会员,需要将其注册成会员
            member = new Member();
            member.setSex(map.get("sex"));
            member.setName(map.get("name"));
            member.setIdCard(map.get("idCard"));
            member.setPhoneNumber(map.get("telephone"));
            member.setRegTime(new Date());

            memberDao.add(member);   //将期注册成一个新的会员,此时一定要将它的id返回
        }

        //判断用户是否重复预约某一套餐,要根据会员id，套餐id，预约日期来查询
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setSetmealId(Integer.parseInt(map.get("setmealId")));
        order.setOrderDate(orderDate);

        List<Order> orderList= orderDao.findByCondition(order);  //如果能查询出来说明重复预约

        if(orderList!=null&&orderList.size()>0){
            return new Result(false,MessageConstant.HAS_ORDERED);
        }

        //开始添加订单信息
        order.setOrderType(map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        orderDao.add(order);   //添加订单信息,返回订单的主键

        //发短信通知用户已经预约成功

        //列新预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        ordersettingDao.updateReservationsByOrderDate(orderSetting);

        //返回订单的id到前台
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }


    @Override
    public Map findById(Integer id) {
        return orderDao.findById(id);
    }
}
